﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HelpCentral.Configuration;
using HelpCentral.Data.Model;
using HelpCentral.Shared.Interfaces;

namespace HelpCentral.Data.Repository
{
    internal static class ClientExtensions
    {
        public static IClient FromRepositoryObject(this Client source)
        {
            if (source == null) return null;

            var c = (IClient)CoreContainer.ResolveNew<IClient>();

            c.ClientId = source.ClientId;
            c.License = source.License;
            c.Name = source.Name;
            c.ActivateDate = source.ActivateDate;
            c.DeactivateDate = source.DeactivateDate;

            return c;
        }

        internal static Client ToRepositoryObject(this IClient source, HelpCentralModelContainer context)
        {
            if (source == null) return null;

            var c = context.Clients.FirstOrDefault(x => x.ClientId == source.ClientId);

            if (c == null)
            {
                c = new Client();
            }

            c.License = source.License;
            c.Name = source.Name;
            c.ActivateDate = source.ActivateDate;
            c.DeactivateDate = source.DeactivateDate;

            return c;
        }
    }
}
